package com.vcg.comment.support.clean.utils;

import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by wuyu on 2016/5/31.
 */
public class HttpClientUtil {

    public static String post(String api_url, Map<String, String> postParameters) {
        try {
            DefaultHttpClient e = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(api_url);
            ArrayList list = new ArrayList();
            Iterator iterator = postParameters.entrySet().iterator();

            while(iterator.hasNext()) {
                Map.Entry response = (Map.Entry)iterator.next();
                list.add(new BasicNameValuePair((String)response.getKey(), (String)response.getValue()));
            }

            if(list.size() > 0) {
                UrlEncodedFormEntity response1 = new UrlEncodedFormEntity(list, "utf-8");
                httpPost.setEntity(response1);
            }

            HttpResponse response2 = e.execute(httpPost);
            String result = EntityUtils.toString(response2.getEntity(), "utf-8");
            return result;
        } catch (Exception var8) {
            var8.printStackTrace();
            return "error";
        }
    }
}
